///geo Module API Module
//

#include <iostream>
#include "geo_api.h"

/// Constructor
Cgeo_API::Cgeo_API()
{
int err_no = 1298; ///Test error code for FMA Server
    geo_API_fault_report_unit_test();
}

/// De-Constructor
Cgeo_API::~Cgeo_API()
{
}

///Process an API request coming from the user interface module (overloaded type 1)
unsigned int Cgeo_API::Process_Module_API_Request(short API_id, unsigned char addr, unsigned char *buf, int nbytes)
{
    switch((egeoAPI_MsgTypes)API_id){

		case GEO_API_READ:
		case GEO_API_WRITE:
		    geo_buf = buf;
		    geo_byte_count = nbytes;
		    geo_addr = addr;
		    geo_error = Process_Module_API_Request(API_id);
		    break;

        default:
            HandleUnknownAPIRequest((egeoAPI_MsgTypes)API_id);
            break;
	}

	return geo_error;
}

///Process an API request coming from the user interface module
unsigned int Cgeo_API::Process_Module_API_Request(short API_id)
{
int result;

    geo_error = GEO_API_NO_ERROR;

    geo_control_Initialise((char *)"/dev/geo_usb0");

    switch((egeoAPI_MsgTypes)API_id){

	case GEO_API_RESET_HW:
            result = reset();
            if( result ){
                geo_error = GEO_API_RESET_HW_ERROR;
                HandleResetHWError(result);
            }
	break;

	case GEO_API_GET_HW_STATUS:
            result = status();
            if( result ){
		///NOTE, result is the result of the API call, not the status of the HW
                geo_error = GEO_API_GET_HW_STATUS_ERROR;
                HandleGetHWStatusError(result);
            }

            if(hw_status){
                printf("HW FAULT CODE 0x%08x\n", hw_status);
                HandleHWFault(result);
	    }
        break;

	case GEO_API_READ:
            result = geo_read(geo_buf);
            if( result ){
                geo_error = GEO_API_READ_ERROR;
                HandleReadHWError(result);
            }
	break;

	case GEO_API_WRITE:
            result = geo_write(geo_buf);
            if( result ){
                geo_error = GEO_API_WRITE_ERROR;
                HandleWriteHWError(result);
            }
	break;

        default:
            HandleUnknownAPIRequest((egeoAPI_MsgTypes)API_id);
            break;
	}

    geo_control_Close();

	return geo_error;
}

///Send an API response to the user interface module, following an API request coming in
GEO_API_Error Cgeo_API::Send_API_Response(short API_id)
{
    geo_API_error = GEO_API_NO_ERROR;

	return geo_API_error;
}

///Send an API request to the user interface module
GEO_API_Error Cgeo_API::Send_API_Request(short API_id)
{
    geo_API_error = GEO_API_NO_ERROR;

	return geo_API_error;
}

///----------------------------------------------------------------------------------------------------
///Error handling for the geo Module HW
///----------------------------------------------------------------------------------------------------

///Handle HW Fault
void Cgeo_API::HandleHWFault(int err_no)
{
    geo_API_fault_report();
}

///Handle Reset HW error
void Cgeo_API::HandleResetHWError(int err_no)
{
    geo_API_fault_report();
}

///Handle Get HW Status error
void Cgeo_API::HandleGetHWStatusError(int err_no)
{
    geo_API_fault_report();
}

//Handle Handle Unknown API Request
void Cgeo_API::HandleUnknownAPIRequest(int err_no)
{
    geo_API_fault_report();
}

//Handle Reset Fault
void Cgeo_API::HandleResetHWFault(int err_no)
{
    geo_API_fault_report();
}

//Handle Read Error
void Cgeo_API::HandleReadHWError(int err_no)
{
    geo_API_fault_report();
}

//Handle Write Error
void Cgeo_API::HandleWriteHWError(int err_no)
{
    geo_API_fault_report();
}

